home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Xconq 7.0d37 / source / kernel / dos.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-12  |  3.4 KB  |  189 lines  |  [TEXT/KAHL]

  1. /* MSDOS-specific code for Xconq.
  2.    Copyright (C) 1994 Ed Boston.
  3.  
  4. Xconq is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.  See the file COPYING.  */
  8.  
  9. #include "conq.h"
  10.  
  11. #include <signal.h>
  12. #include <time.h>
  13. #include <sys/types.h>
  14.  
  15. typedef void (*fptr)();
  16.  
  17. #ifndef XCONQLIB
  18. #define XCONQLIB "../lib"
  19. #endif
  20.  
  21. char *
  22. default_library_filename()
  23. {
  24.     return XCONQLIB;
  25. }
  26.  
  27. char *
  28. news_filename()
  29. {
  30.     make_pathname(xconqlib, NEWSFILE, "", spbuf);
  31.     return spbuf;
  32. }
  33.  
  34. char *
  35. saved_game_filename()
  36. {
  37.     return "save.xcq";
  38. }
  39.  
  40. char *
  41. checkpoint_filename()
  42. {
  43.     return "checkpnt.xcq";
  44. }
  45.  
  46. char *
  47. error_save_filename()
  48. {
  49.     return "errorsav.xcq";
  50. }
  51.  
  52. char *
  53. statistics_filename()
  54. {
  55.     return "stats.xcq";
  56. }
  57.  
  58. /* Attempt to open a library file. */
  59.  
  60. FILE *
  61. open_library_file(module)
  62. Module *module;
  63. {
  64.     FILE *fp = NULL;
  65.  
  66.     /* Don't try to do on anon modules? */
  67.     if (module->name == NULL) return NULL;
  68.     /* Generate library pathname. */
  69.     make_pathname(xconqlib, module->name, "g", spbuf);
  70.     /* Now try to open the file. */
  71.     if ((fp = fopen(spbuf, "r")) != NULL) {
  72.         /* Remember the filename where we found it. */
  73.         module->filename = copy_string(spbuf);
  74.     } else {
  75.         /* Extra hack to find experimental games. */
  76.         make_pathname("../lib2", module->name, "g", spbuf);
  77.         if ((fp = fopen(spbuf, "r")) != NULL) {
  78.             /* Remember the filename where we found it. */
  79.             module->filename = copy_string(spbuf);
  80.         }
  81.     }
  82.     return fp;
  83. }
  84.  
  85. FILE *
  86. open_explicit_file(module)
  87. Module *module;
  88. {
  89.      if (module->filename == NULL) return NULL;
  90.      return (fopen(module->filename, "r"));
  91. }
  92.  
  93. FILE *
  94. open_library_file(filename)
  95. char *filename;
  96. {
  97.     FILE *fp = NULL;
  98.  
  99.     /* Don't try to do on anon modules? */
  100.     /* Generate library pathname. */
  101.     make_pathname(xconqlib, filename, NULL, spbuf);
  102.     /* Now try to open the file. */
  103.     if ((fp = fopen(spbuf, "r")) != NULL) {
  104.     } else {
  105.     }
  106.     return fp;
  107. }
  108.  
  109. void
  110. make_pathname(path, name, extn, pathbuf)
  111. char *path, *name, *extn, *pathbuf;
  112. {
  113.     sprintf(pathbuf, "");
  114.     if (!empty_string(path)) {
  115.         sprintf(pathbuf+strlen(pathbuf), "%s/", path);
  116.     }
  117.     sprintf(pathbuf+strlen(pathbuf), "%s", name);
  118.     /* Don't add a second identical extension, but do add if extension
  119.         is different (in case we want "foo.12" -> "foo.12.g" for instance) */
  120.     if (strrchr(name, '.') && strcmp((char *) strrchr(name, '.')+1, extn) == 0)
  121.         return;
  122.     if (!empty_string(extn)) {
  123.         sprintf(pathbuf+strlen(pathbuf), ".%s", extn);
  124.     }
  125. }
  126.  
  127. /* Remove a saved game from the system. */
  128.  
  129. remove_saved_game()
  130. {
  131.     unlink(saved_game_filename());
  132. }
  133.  
  134. /* Default behavior on explicit kill. */
  135.  
  136. void
  137. stop_handler(x)
  138. int x;
  139. {
  140.     close_displays();
  141.     exit(1);
  142. }
  143.  
  144. /* This routine attempts to save the state before dying. */
  145.  
  146. void
  147. crash_handler(sig)
  148. int sig;
  149. {
  150.     static int already_been_here = FALSE;
  151.  
  152.     if (!already_been_here) {
  153.         already_been_here = TRUE;
  154.         close_displays();
  155.         printf("Fatal error encountered. Signal %d\n", sig);
  156.         write_entire_game_state("xconq.ack");
  157.     }
  158.     abort();
  159. }
  160.  
  161. void
  162. init_signal_handlers(void)
  163. {
  164.     signal(SIGINT, stop_handler);
  165.     signal(SIGSEGV, crash_handler);
  166.     signal(SIGFPE, crash_handler);
  167.     signal(SIGILL, crash_handler);
  168.     signal(SIGINT, crash_handler);
  169.     signal(SIGTERM, crash_handler);
  170. }
  171.  
  172.  
  173. time_t reallasttime = (time_t) 0;
  174. time_t realcurtime;
  175.  
  176. int
  177. n_seconds_elapsed(n)
  178. int n;
  179. {
  180.     time(&realcurtime);
  181.     if (realcurtime > reallasttime + (n - 1)) {
  182.         reallasttime = realcurtime;
  183.         return TRUE;
  184.     } else {
  185.         return FALSE;
  186.     }
  187. }
  188.  
  189.